import Vue from 'vue'
import Router from 'vue-router'
import Index from '@/components/Index'
import Home from '@/components/Home'
import Canvas from '@/components/Canvas'
import NodeManager from '@/components/NodeManager'
import CategoriesManager from '@/components/CategoriesManager'
import User from '@/components/User'
import Seach from '@/components/Seach'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      component: Index
    },
    {
      path: '/Seach/:content',
      component: Seach
    },
    {
      path: '/Home',
      name: 'Home',
      component: Home,
      redirect: '/Canvas',
      children: [
        { path: '/Canvas', component: Canvas },
        { path: '/Canvas/:classId', component: Canvas },
        { path: '/NodeManager', component: NodeManager },
        { path: '/CategoriesManager', component: CategoriesManager },
        { path: '/User', component: User }
      ]
    }
  ]
})

// 路由全局导航守卫
router.beforeEach((to, from, next) => {
  const user = ['/User']
  const manager = ['/NodeManager', '/CategoriesManager']
  for (let i = 0; i < user.length; i++) {
    if (to.fullPath === user[i]) {
      if (window.sessionStorage.token) {
        return next()
      } else {
        return next('/Home')
      }
    }
  }
  for (let i = 0; i < manager.length; i++) {
    if (to.fullPath === manager[i]) {
      if (window.sessionStorage.admin === '1') {
        return next()
      } else {
        return next('/Home')
      }
    }
  }
  next()
})

export default router
